--[[ Автор: O.Faustle Мод: Call of Misery 1.0c Адаптация: Deployable Tent (http://www.moddb.com/mods/call-of-chernobyl/addons/deployable-tent) --]] local ini_tent local TentBagRadius, TentBagPlaceable, TentBagSections local SleepBagEnable, SleepBagRadius, SleepBagPlaceable, SleepBagGameStart, SleepBagSections, SleepBagPlaceableSections -- for m.data local spawned_tents = {} local spawned_sleepbags = {} function start_timer(obj) local enabled_animations = ui_options.get("video/player/animations") local iTimer = enabled_animations and (time_global() + 4000) or time_global() local function check_timer() return time_global() > iTimer end if (obj:section() == "itm_tent") then level.add_call(check_timer, itm_tent_func) end if (obj:section() == "itm_tent_bag") then level.add_call(check_timer, itm_tent_bag_func) end end function itm_tent_func() local sec = "itm_tent" if (TentBagPlaceable) then local pos = db.actor:position() local dir = db.actor:direction() pos = pos:add(dir:mul(3)) local ph_sec = ini_sys:r_string_ex(sec,"placeable_section") local se_obj = alife_create(ph_sec or "ph_tent", pos, 1, db.actor:game_vertex_id()) level.map_add_object_spot_ser(se_obj.id, "ui_pda2_actor_sleep_location", ui_item.get_sec_name(sec)) spawned_tents[se_obj.id] = true alife_create_item("itm_tent_bag", db.actor) else alife_create_item(sec, db.actor) end return end function get_nearby_tent(dist, result) for id, v in pairs(spawned_tents) do local obj = level.object_by_id(id) if obj and (v == true) then local sec = obj:section() if (sec == "ph_tent") then local tent_distance = obj:position():distance_to(db.actor:position()) --printf("Tent found | id: " .. tostring(id) .. " | distance: " .. tostring(tent_distance)) if (tent_distance < dist) then if (result == true) then return id else return true end end return false end end end end function itm_tent_bag_func() local id = get_nearby_tent(3.5,true) local se_obj = id and alife_object(id) if se_obj then local obj_tent = db.actor:object("itm_tent_bag") local se_tent = obj_tent and obj_tent:id() and alife_object(obj_tent:id()) if se_tent then alife_create_item("itm_tent", db.actor) alife_release(se_obj) alife_release(se_tent) spawned_tents[id] = nil end end end function use_compression_bag(obj_c,dist) for id, v in pairs(spawned_sleepbags) do local obj = level.object_by_id(id) if obj and (v == true) then local sec = obj:section() if (sec == "ph_sleepbag") then local sp_distance = obj:position():distance_to(db.actor:position()) --printf("Sleepbag found | id: " .. tostring(id) .. " | distance: " .. tostring(sp_distance)) if (sp_distance < dist) then local se_comp = obj_c and obj_c:id() and alife_object(obj_c:id()) local se_sleepbag = alife_object(id) if se_comp and se_sleepbag then alife_create_item("itm_sleepbag", db.actor) alife_release(se_comp) alife_release(se_sleepbag) spawned_sleepbags[id] = nil -- actor_effects.play_item_fx("itm_sleepbag") break end end end end end end function get_spawned_tents() return spawned_tents end function get_spawned_sleepbags() return spawned_sleepbags end --=======================================< Callbacks >=======================================-- local function actor_on_item_use(obj) local sec = obj:section() -- Tent or Tent bag0 if (sec == "itm_tent") or (sec == "itm_tent_bag" and get_nearby_tent(3.5,true)) then actor_effects.play_item_fx("itm_sleepbag") start_timer(obj) end -- Sleeping bag if (SleepBagEnable and SleepBagSections[sec]) then if (SleepBagPlaceable) then local ph_sec = ini_tent:r_string_ex(sec,"placeable_section") local se_obj = alife_create(ph_sec or "ph_sleepbag",db.actor:position(),db.actor:level_vertex_id(),db.actor:game_vertex_id()) level.map_add_object_spot_ser(se_obj.id, "ui_pda2_actor_sleep_location", ui_item.get_sec_name(sec)) spawned_sleepbags[se_obj.id] = true alife_create_item("compression_bag", db.actor) else local se_obj = alife_create_item(sec, db.actor) hide_hud_inventory() local ui = ui_sleep_dialog.sleep_bag(se_obj,sec) end end -- Compression bag if (sec == "compression_bag") then use_compression_bag(obj,1.5) end end local function physic_object_on_use_callback(obj,who) -- bind_physic_object use_callback if (SleepBagEnable and SleepBagPlaceableSections[obj:section()]) then hide_hud_inventory() ui_sleep_dialog.sleep_bag(obj:id(),SleepBagPlaceableSections[obj:section()]) end end local function save_state(m_data) m_data.spawned_tents = spawned_tents m_data.spawned_sleepbags = spawned_sleepbags end local function load_state(m_data) spawned_tents = m_data.spawned_tents or {} spawned_sleepbags = m_data.spawned_sleepbags or {} end local function on_game_load() if (SleepBagEnable and SleepBagGameStart) then local sleepbag_id = load_var(db.actor,"itm_sleepbag_id",nil) if (not sleepbag_id) then local se_obj = alife_create_item("itm_sleepbag", db.actor) save_var(db.actor,"itm_sleepbag_id",se_obj.id) end end end function on_game_start() ini_tent = itms_manager.ini_manager -- Tent mod TentBagRadius = ini_tent:r_float_ex("tent_i_bag","unsafe_radius") or 5000 TentBagPlaceable = ini_tent:r_bool_ex("tent_i_bag","use_placeable",true) TentBagSections = utils_data.collect_section(ini_tent,"tent_i_bag_sections",true) -- Sleep bag mod SleepBagEnable = ini_tent:r_bool_ex("sleeping_bag","enable",true) SleepBagRadius = SleepBagEnable and ini_tent:r_float_ex("sleeping_bag","unsafe_radius") or 5000 SleepBagPlaceable = SleepBagEnable and ini_tent:r_bool_ex("sleeping_bag","use_placeable",true) SleepBagGameStart = SleepBagEnable and ini_tent:r_bool_ex("sleeping_bag","have_at_start",false) SleepBagSections = SleepBagEnable and utils_data.collect_section(ini_tent,"sleeping_bag_sections",true) SleepBagPlaceableSections = SleepBagEnable and utils_data.collect_section(ini_tent,"sleeping_bag_ph_sections",true) --RegisterScriptCallback("on_game_load",on_game_load) RegisterScriptCallback("actor_on_item_use",actor_on_item_use) RegisterScriptCallback("physic_object_on_use_callback",physic_object_on_use_callback) RegisterScriptCallback("save_state",save_state) RegisterScriptCallback("load_state",load_state) end function str_use() return 'st_item_use' end function func_use_bag(obj) if obj then actor_on_item_use(obj) CreateTimeEvent(0, obj:name(), 0, function(x) local se = alife_object(x) _ = se and alife_release(se) return true end, obj:id()) end end function func_use_tent(obj) if obj then actor_on_item_use(obj) CreateTimeEvent(0, obj:name(), 0, function(x) local se = alife_object(x) _ = se and alife_release(se) return true end, obj:id()) end end